package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.gold.GoldWithdrawalDto;
import com.java110.dto.integral.IntegralDto;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.intf.acct.IIntegralWithdrawalV1InnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.po.integral.IntegralWithdrawalPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("applyIntegralWithdrawalBmoImpl")
public class ApplyIntegralWithdrawalBmoImpl implements IMallCommonApiBmo {
    public static final String CODE_PREFIX_ID = "10";


    @Autowired
    private IIntegralWithdrawalV1InnerServiceSMO integralWithdrawalV1InnerServiceSMOImpl;

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;


    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "integralId", "请求报文中未包含integralId");
        Assert.hasKeyAndValue(reqJson, "applyAmount", "请求报文中未包含applyAmount");
        Assert.hasKeyAndValue(reqJson, "applyUserName", "请求报文中未包含applyUserName");
        Assert.hasKeyAndValue(reqJson, "applyUserTel", "请求报文中未包含applyUserTel");
        Assert.hasKeyAndValue(reqJson, "remark", "请求报文中未包含remark");
        Assert.hasKeyAndValue(reqJson, "bankNum", "请求报文中未包含backNum");
        Assert.hasKeyAndValue(reqJson, "bankNumName", "请求报文中未包含backNumName");
        Assert.hasKeyAndValue(reqJson, "bankName", "请求报文中未包含bankName");
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含communityId");

    }

    @Override
    @Java110Transactional
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {
        String userId = reqJson.getString("propertyUserId");

        IntegralDto integralDto = new IntegralDto();
        integralDto.setIntegralId(reqJson.getString("integralId"));
        List<IntegralDto> integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);
        Assert.listOnlyOne(integralDtos, "积分账户不存在");


        if (Double.parseDouble(integralDtos.get(0).getAmount()) < reqJson.getDouble("applyAmount")) {
            throw new CmdException("金额不做");
        }


        IntegralWithdrawalPo integralWithdrawalPo = BeanConvertUtil.covertBean(reqJson, IntegralWithdrawalPo.class);
        integralWithdrawalPo.setIwId(GenerateCodeFactory.getGeneratorId(CODE_PREFIX_ID));
        integralWithdrawalPo.setState(GoldWithdrawalDto.STATE_W);
        integralWithdrawalPo.setStateMsg("待审核");
        integralWithdrawalPo.setApplyUserId(userId);
        integralWithdrawalPo.setShopId(reqJson.getString("communityId"));
        integralWithdrawalPo.setAmount(reqJson.getString("applyAmount"));
        integralWithdrawalPo.setIntegralName(integralDtos.get(0).getIntegralName());
        int flag = integralWithdrawalV1InnerServiceSMOImpl.saveIntegralWithdrawal(integralWithdrawalPo);

        if (flag < 1) {
            throw new CmdException("保存数据失败");
        }

        context.setResponseEntity(ResultVo.success());
    }
}
